package com.hhit.service.imp;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.hhit.dao.TUserMapper;
import com.hhit.pojo.TUser;
import com.hhit.pojo.TUserExample;
import com.hhit.service.UserService;


@Service
public class UserServiceImp implements UserService {

	@Resource
	private TUserMapper userMapper;
	
	@Autowired
	private TUserExample tuserExample;

	public TUser login(String name, String passwd) {
		tuserExample.clear();
		tuserExample.createCriteria().andUsernameEqualTo(name).andPasswordEqualTo(passwd);
		List<TUser> userList = userMapper.selectByExample(tuserExample);
		return null != userList && userList.size() > 0 ? userList.get(0) : null;
	}

	public List<TUser> getAll() {
		return userMapper.selectByExample(null);
	}

	@Override
	public void delUser(Integer id) {
		userMapper.deleteByPrimaryKey(id);	
	}

	@Override
	public void addUser(TUser user) {
		user.setPassword("123");

		userMapper.insertSelective(user);
	}

	@Override
	public TUser getUser(Integer id) {
		return userMapper.selectByPrimaryKey(id);
	}

	@Override
	public void updateUser(TUser user) {
		userMapper.updateByPrimaryKeySelective(user);
		
	}

	@Override
	public boolean checkNameIsExisted(String name) {
		TUser user = new TUser();
		user.setUsername(name);
		tuserExample.clear();
		tuserExample.createCriteria().andUsernameEqualTo(name);
		List<TUser> userList = userMapper.selectByExample(tuserExample);
		return null != userList && userList.size() > 0 ? false : true;
	}



}
